/**
 * Created by developer on 17-2-22.
 */
var $id;
var $status;
var $rightSlider = $(".aside-slider-content");
var $cover = $(".masking");
var $form = $(".container-add-product-new");
var $advanced = $("#advanced");
$(function () {
    //新增商品
    $(".button-add-product").on("click", function () {
        $form.children(".panel_header").html("新增商品适用年龄<span class='glyphicon glyphicon-remove cancel'></span>");
        modifyAddOrModifyWindow();
        //"保存并添加下一个"按钮显示
        $("#save_add_next").show();
        $advanced.css("display", "none");
    });

    //新增和修改商品弹框高级属性展开收起
    $(".more-end").children(".icon").on("click", function () {
        if ($advanced.css("display") == "none") {
            $(".more-end").children(".more-text").css("display", "none");
            $advanced.css("display", "block");
            $(this).html("&#xe623;");
        } else {
            $(".more-end").children(".more-text").css("display", "block");
            $advanced.css("display", "none");
            $(this).html("&#xe605;");
        }
    });

    //列表点击事件，出现侧栏
    $(".list-item").on("click", function () {
        var $currentId = $(this).attr("id");
        expandOrCloseSidebar(getStatus($currentId));
    });

    //关闭侧栏
    $rightSlider.find(".cancel").on("click", function () {
        $rightSlider.animate({right: "-400px"});
        $status = "close";
    });

    //编辑商品
    $("#modify_product").on("click", function () {
        $form.children(".panel_header").html("修改商品适用年龄<span class='glyphicon glyphicon-remove cancel'></span>");
        modifyAddOrModifyWindow();
        //"保存并添加下一个"按钮隐藏
        $("#save_add_next").hide();
        $advanced.css("display", "none");
    });

    //删除商品
    $("#delete_product").on("click", function () {
        modifyDisplayBlock($(".container-delete-product"));
    });

    //关闭删除框
    $(".container-delete-product").find(".cancel").on("click", function () {
        modifyDisplayNone($(".container-delete-product"));
    });
});

//获取页面侧栏的打开或关闭的状态
function getStatus(currentId) {
    if ($id == currentId) {
        $status = "close";
        $id = "";
    } else {
        $status = "open";
        $id = currentId;
    }
    return $status;
}

//通过id对比打开或关闭页面侧栏
function expandOrCloseSidebar($status) {
    console.log($status);
    if ($status == "close") {
        $rightSlider.animate({right: "-400px"});
    } else if ($status == "open") {
        $rightSlider.animate({right: "0px"});
    }
}

//修改css样式display为block
function modifyDisplayBlock(obj) {
    obj.css("display", "block");
    $cover.css("display", "block");
}

//修改css样式display为none
function modifyDisplayNone(obj) {
    obj.css("display", "none");
    $cover.css("display", "none");
}

//修改添加\修改商品弹出窗
function modifyAddOrModifyWindow() {
    $form.css({
        "display": "flex",
        "display": "-webkit-flex"
    });
    $cover.css("display", "block");
    //关闭弹出框
    $form.find(".cancel").on("click", function () {
        modifyDisplayNone($form);
    });
}